package com.hugesoft.jxgs.activity;

import android.app.Notification;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hugesoft.jxgs.R;
import com.hugesoft.jxgs.activity.base.BaseActivity;
import com.hugesoft.jxgs.utils.CheckInputUtil;
import com.hugesoft.logic.managers.LogicManager;
import com.hugesoft.logic.managers.common.Constans;
import com.hugesoft.utils.Util;

public class LoginActivity extends BaseActivity implements OnClickListener {
	private final String TAG = "LoginActivity";

	private Button mBtnReg, mBtnLogin;

	private EditText mEditPoliceNo, mEditPwd;

	private long mExitTime = 0;

	private TextView tvIMEI;
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			dismissDialog();
			switch (msg.what) {
			case Constans.LOGIN_SUCCESS:
				// 登录成功，摘取该用户的信息
				Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_LONG);

				Intent intent = new Intent(LoginActivity.this,
						SelectTypeActivity.class);
				startActivity(intent);
				break;
			case Constans.LOGIN_FAILED:
				Toast.makeText(LoginActivity.this,
						msg.obj == null ? "登陆失败" : msg.obj.toString(),
						Toast.LENGTH_SHORT).show();
				// initUI();
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		addIconToStatusbar(R.drawable.ic_launcher, Notification.FLAG_NO_CLEAR,
				"欢迎来到江西高速管理系统");
		boolean rst = false;// LogicManager.policeManager().LoginWithSavedData(mHandler);
		if (!rst) {
			initUI();
		} else {
			showWaitDialog("登陆中");
		}
	}

	private void initUI() {
		setContentView(R.layout.activitylogin);

		findViewById(R.id.backImg).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showAlertDialog("提示", "确定退出程序吗?", "确定", "取消",
						new LeftButtonclickListener() {

							@Override
							public void onLeftBtnClick() {
								// TODO Auto-generated method stub
								backToDesk();
							}
						}, new RightButtonclickListener() {

							@Override
							public void onRightBtnClick() {
								// TODO Auto-generated method stub

							}
						});
			}
		});

		mEditPoliceNo = (EditText) findViewById(R.id.username_edittext);
		mEditPwd = (EditText) findViewById(R.id.pwd_edittext);

		mBtnLogin = (Button) findViewById(R.id.login);
		mBtnReg = (Button) findViewById(R.id.regBtn);

		mBtnLogin.setOnClickListener(this);
		mBtnReg.setOnClickListener(this);

//		mEditPwd.setText("123456");
//		mEditPoliceNo.setText("123456");

		tvIMEI = (TextView) findViewById(R.id.tvIMEI);
		tvIMEI.setVisibility(View.GONE);
		setIMEI();
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.login:
			if (checkpolice()) {
				LogicManager.policeManager().login(
						mEditPoliceNo.getText().toString(),
						mEditPwd.getText().toString(), mHandler);
			}
			break;
		case R.id.regBtn:
			Intent intent = new Intent(LoginActivity.this, RegActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	private boolean checkpolice() {
		boolean ret = true;

		if (mEditPoliceNo.getText() == null
				|| !CheckInputUtil.ispolicenoverified(mEditPoliceNo.getText()
						.toString())) {
			Util.showToast(LoginActivity.this, "警号错了哦，亲");
			ret = false;
			return ret;
		}

		if (mEditPwd.getText() == null
				|| !CheckInputUtil.ispolicepasswordverified(mEditPwd.getText()
						.toString())) {
			Util.showToast(LoginActivity.this, "密码错了哦，亲");
			ret = false;
			return ret;
		}

		return ret;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			if ((System.currentTimeMillis() - mExitTime) > 2000) {
				Toast.makeText(getApplicationContext(), "再按一次返回键回到桌面",
						Toast.LENGTH_SHORT).show();
				mExitTime = System.currentTimeMillis();
			} else {
				backToDesk();
			}
			return true;
		}
		return true;
	}

	private void setIMEI() {
		TelephonyManager tm = (TelephonyManager) this
				.getSystemService(TELEPHONY_SERVICE);

		tvIMEI.setText("IMEI:"+tm.getDeviceId());// String
	}
}
